<?php
namespace Wap\Controller;

use Think\Controller;
use Think\Model;
use User\Model\ActivityModel;
use User\Model\ActivityRegisterModel;
use Wap\Service\WeixinService;

class ActivitydsController extends WapController
{
    public $_activity;//活动信息
    public $wxuser;//活动绑定的微信号
    public $user;//微信用户
    public $is_mobile;

    public function __construct()
    {
        parent::__construct();

        if (!headers_sent() && // 如果页面头部信息还没有输出--
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展--
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP--
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }

        $this->handleCriteriaMember();
        $this->handleCriteriaWxuser();
        $this->is_mobile = $this->isMobile();
        if ($this->is_mobile) {
            $wxJSApi = WeixinService::buildWXJSApi($this->wxuser);
            $wxConfig = array(
                'appId' => $this->wxuser['appid'],
                'timestamp' => $wxJSApi['timestamp'],
                'nonceStr' => $wxJSApi['noncestr'],
                'signature' => $wxJSApi['sha1']
            );
            $this->assign('wxConfig', $wxConfig);
        }
    }

    public function index()
    {
        header("Content-type: text/html; charset=utf-8");
            die('活动已结束！');
        $activity = $this->_activity;
        $status = 1;//1正常，2还未开始，3过期
        if (empty($activity) || ($activity['start_date'] > date("Y-m-d H:i:s"))) {
            $status = 2;
        }
        if (!empty($activity) && $activity['end_date'] < date("Y-m-d H:i:s")) {
            $status = 3;
        }
        $this->assign("status", $status);
        $this->assign("count", $activity['reserve']);
        $this->display();
    }

    public function register()
    {
        $params = I("post.", "", "");
        if (empty($params['id'])) {
            $this->ajaxReturn(array('status' => -1, "result" => "请选择活动"));
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(array('status' => -2, "result" => "请输入您的姓名"));
        }
        if (empty($params['mobile'])) {
            $this->ajaxReturn(array('status' => -3, "result" => "请输入您的联系方式"));
        }
        if (empty($params['company'])) {
            $this->ajaxReturn(array('status' => -4, "result" => "请输入您的公司名称"));
        }
        if (empty($params['position'])) {
            $this->ajaxReturn(array('status' => -5, "result" => "请输入您的职位"));
        }
        if (empty($params['note'])) {
            $this->ajaxReturn(array('status' => -6, "result" => "请选择您要参加的活动"));
        }
        if (empty($this->wxuser['token'])) {
            $this->ajaxReturn(array('status' => -7, "result" => "活动有误"));
        }
        $cond['mobile'] = $params['mobile'];
        $cond['token'] = $this->wxuser['token'];
        $cond['act_id'] = $params['id'];
        $register = M("activity_register")->where($cond)->find();
        if (!empty($register)) {
            $this->ajaxReturn(array('status' => -8, "result" => "该手机号码已被注册"));
        }
        $data['act_id'] = $params['id'];
        $data['token'] = $this->wxuser['token'];
        $data['name'] = $params['name'];
        $data['company'] = $params['company'];
        $data['position'] = $params['position'];
        $data['mobile'] = $params['mobile'];
        $data['note'] = $params['note'];
        $data['status'] = ActivityRegisterModel::STATUS_PENDING;
        M("activity_register")->add($data);
        $this->ajaxReturn(array('status' => 1, "result" => "您的报名主办方已收到。"));
    }

    /*
    *是否合法活动
    */
    private function handleCriteriaMember()
    {
        $cond['act_id'] = IS_GET ? I("get.id", -1, "") : I("post.id", -1, "");
        $cond['status'] = ActivityModel::STATUS_ACTIVE;
        $this->_activity = M("activity")->where($cond)->find();
        if (!empty($this->_activity)) {
            $reserve = $this->_activity['reserve'];
            if($reserve == null){
                $this->_activity['reserve'] = 1;
                M('activity')->where(['act_id' => $cond['act_id']])->save(array('reserve' => 1));
            }else{
                $this->_activity['reserve'] += 1;
                M('activity')->where(['act_id' => $cond['act_id']])->setInc('reserve', 1);
            }
        }
    }

    /*
    *微信公众号信息
    */
    private function handleCriteriaWxuser()
    {
        $activity = $this->_activity;
        if (isset($activity['token'])) {
            $this->wxuser = WeixinService::getWxuser($this->_activity['token']);
        }
    }
}
